package com.smartview.jboss.spring.model;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;

import com.smartview.jboss.spring.dto.NewsDTO;
import com.smartview.jboss.spring.dto.WeatherDTO;

@Service
public class DataParser {
	public WeatherDTO parseWeather(String zip, InputStream in) throws DocumentException {
//		System.out.println("We are parsing the data from Yahoo");
		Logger logger = Logger.getLogger(this.getClass());
		logger.debug("We are parsing the data from Yahoo");
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
		map.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
		
		DocumentFactory factory = new DocumentFactory();
		factory.setXPathNamespaceURIs(map);
		
		SAXReader reader = new SAXReader(factory);
		
		// Now we want to read the data
		Document doc = reader.read(in);
		String temp = doc.valueOf("/rss/channel/item/yweather:condition/@temp");
		String city = doc.valueOf("/rss/channel/yweather:location/@city");
		String state = doc.valueOf("/rss/channel/yweather:location/@region");
		String country = doc.valueOf("/rss/channel/yweather:location/@country");
		String longitude = doc.valueOf("/rss/channel/item/geo:long/.");
		String latitude = doc.valueOf("/rss/channel/item/geo:lat/.");

		WeatherDTO weather = new WeatherDTO();
		
		weather.setZip(zip);
		weather.setCity(city);
		weather.setState(state);
		weather.setCountry(country);
		weather.setLongitude(longitude);
		weather.setLatitude(latitude);
		weather.setTemperature(temp);
		
		return weather;
		
	}
	
	public ArrayList<NewsDTO> parseNews(String key, InputStream in) throws DocumentException {
		Logger logger = Logger.getLogger(this.getClass());
		logger.debug("We are parsing the data from Yahoo");
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("yweather", "http://xml.news.yahoo.com/ns/rss/1.0");
		map.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
		DocumentFactory factory = new DocumentFactory();
		factory.setXPathNamespaceURIs(map);
		
		SAXReader reader = new SAXReader(factory);
		// Now we want to read the data
		Document doc = reader.read(in);
		Element root = doc.getRootElement();
	
		ArrayList<NewsDTO> a = new ArrayList<NewsDTO>();
		
		for(int i=0; i<root.selectNodes("/rss/channel/item/title").size(); i++){
			NewsDTO news = new NewsDTO();
			news.setTitle(root.selectNodes("/rss/channel/item/title/text()").get(i).toString().split("\"")[1]);
			news.setLink(root.selectNodes("/rss/channel/item/link/text()").get(i).toString().split("\"")[1]);
			news.setSource(root.selectNodes("/rss/channel/item/source/text()").get(i).toString().split("\"")[1]);
			news.setPubDate(root.selectNodes("/rss/channel/item/pubDate/text()").get(i).toString().split("\"")[1]);
			a.add(news);
		}

		return a;
		
	}
	public ArrayList<NewsDTO> parseCompany(String key, InputStream in) throws DocumentException {
		Logger logger = Logger.getLogger(this.getClass());
		logger.debug("We are parsing the data from Yahoo");
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("yweather", "http://xml.news.yahoo.com/ns/rss/1.0");
		map.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
		DocumentFactory factory = new DocumentFactory();
		factory.setXPathNamespaceURIs(map);
		
		SAXReader reader = new SAXReader(factory);
		// Now we want to read the data
		Document doc = reader.read(in);
		Element root = doc.getRootElement();
	
		ArrayList<NewsDTO> a = new ArrayList<NewsDTO>();
		for(int i=0; i<root.selectNodes("/rss/channel/item/title").size(); i++){
			NewsDTO news = new NewsDTO();
			news.setTitle(root.selectNodes("/rss/channel/item/title/text()").get(i).toString().split("\"")[1]);
			news.setLink(root.selectNodes("/rss/channel/item/link/text()").get(i).toString().split("\"")[1]);
			news.setPubDate(root.selectNodes("/rss/channel/item/pubDate/text()").get(i).toString().split("\"")[1]);
			a.add(news);
		}

		return a;
		
	}
	
}
